Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy: support dynamic port number #289

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lelemka0
Copy link

@lelemka0 lelemka0 commented Jan 23, 2025

This PR delays address parsing in some case to make dynamic port numbers work properly.
If dial addresses contains { and }, address parsing in Upstream.provision will be skipped then done later in Handler.dialPeers after all placeholders are replaced, and active health checks will be ignored.

Example Caddyfile:

layer4 {
	:2000 {
		@test tls sni_regexp ^([0-9]{1})\.test$
		route @test {
			proxy {
				upstream 127.0.0.1:800{tls.regexp.1}
			}
		}
	}
}

@lelemka0
Copy link
Author

lelemka0 commented Feb 8, 2025

Will this be merged, or will other plans be considered for dynamic ports?

Copy link
Owner

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, been super busy... I haven't tested this myself but I trust you have? I just have one request for a comment, as I'm not sure if that new exported function is necessary.

@@ -30,12 +30,23 @@ import (
"github.com/mholt/caddy-l4/layer4"
)

func ParseAddress(addr string) (*caddy.NetworkAddress, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a godoc to this? to explain why it is needed? (I'm not entirely sure myself, just by looking at it, since it uses only exported methods)

@lelemka0
Copy link
Author

This was my mistake about the exported function, it's not used outside and is unlikely to be used outside.
I have modified it to unexported and added doc.

I have been using it and it is working fine for two weeks. For a simple test, use the following config:

{
	debug
	cert_issuer internal
	layer4 {
		:2000 {
			@test tls sni_regexp ^(\d{1})\.test$
			route @test {
				proxy {
					upstream 127.0.0.1:820{tls.regexp.1}
				}
			}
		}
	}
}

:8200 {
	tls {
		on_demand
	}
	respond OK 200
}
:8201 {
	tls {
		on_demand
	}
	respond OK-1 200
}
curl --insecure --resolve "1.test:2000:127.0.0.1" "https://1.test:2000/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants